package com.dhec.ptms.epr.db;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dhec.ptms.epr.model.SourceRecordDetail;
import com.ionglobal.db.DBI;
import com.ionglobal.lib.SysException;

public class SourceRecordDetailDB {
	private static final Logger logger = LoggerFactory
			.getLogger(SourceRecordDetailDB.class);
	
	
	public List<SourceRecordDetail> list(DBI dbi, Map<String, String> params, int page, int pagesize){
		ArrayList<SourceRecordDetail> list = new ArrayList<SourceRecordDetail>(0);
		
		try {
			String sql = "select * from EPR_SourceRecordDetail where 1=1";
			sql += getSqlWhereStr(params, dbi);
			sql+=" order by id desc";
			dbi.skipToPage(pagesize, page, sql);
			while(dbi.rsNext()){
				SourceRecordDetail bean = new SourceRecordDetail();
				this.handleData(dbi, bean);
				list.add(bean);
			}
			dbi.rsClose();
		} catch (SysException e) {
			logger.error("SourceRecordDetailDB.list(DBI, Map, int, int) Error!", e);
		}
		
		return list;
	}
	
	public long count(DBI dbi, Map<String, String> params){
		long c=0;
		try {
			String sql = "select count(*) from EPR_SourceRecordDetail where 1=1"+getSqlWhereStr(params, dbi);
			dbi.execQuery(sql);
			if(dbi.rsNext()){
				c = dbi.rsGetLong(1);
			}
			dbi.rsClose();
		} catch (SysException e) {
			logger.error("SourceRecordDetailDB.count(DBI, params) Error!", e);
		}
		
		return c;
	}
	
	public void handleData(DBI dbi, SourceRecordDetail bean) throws SysException{
		bean.setGe(new BigDecimal(dbi.rsGetDouble("ge")));
		bean.setGf(new BigDecimal(dbi.rsGetDouble("gf")));
		bean.setId(dbi.rsGetLong("id"));
		bean.setNmin(dbi.rsGetInt("nmin"));
		bean.setP(new BigDecimal(dbi.rsGetDouble("p")));
		bean.setPAir(new BigDecimal(dbi.rsGetDouble("p_air")));
		bean.setPb(new BigDecimal(dbi.rsGetDouble("pb")));
		bean.setPeo(new BigDecimal(dbi.rsGetDouble("peo")));
		bean.setPExh(new BigDecimal(dbi.rsGetDouble("p_exh")));
		bean.setPhi(new BigDecimal(dbi.rsGetDouble("phi")));
		bean.setPOil(new BigDecimal(dbi.rsGetDouble("p_oil")));
		bean.setRecordid(dbi.rsGetInt("recordid"));
		bean.setRedfac(new BigDecimal(dbi.rsGetDouble("redfac")));
		bean.setTAir(new BigDecimal(dbi.rsGetDouble("t_air")));
		bean.setTeo(new BigDecimal(dbi.rsGetDouble("teo")));
		bean.setTExh(new BigDecimal(dbi.rsGetDouble("t_exh")));
		bean.setTfuel(new BigDecimal(dbi.rsGetDouble("tfuel")));
		bean.setTOil(new BigDecimal(dbi.rsGetDouble("t_oil")));
		bean.setTorque(new BigDecimal(dbi.rsGetDouble("torque")));
		bean.setTwi(new BigDecimal(dbi.rsGetDouble("twi")));
		bean.setTwo(new BigDecimal(dbi.rsGetDouble("two")));
		bean.setReportid(dbi.rsGetInt("reportid"));
		bean.setEngineno(dbi.rsGetString("engineno"));
	}
	
	private String getSqlWhereStr(Map<String,String> params, DBI dbi){
		String sql="";
		if(params.containsKey("nmin")){
			String nmin = params.get("nmin");
			if(nmin.indexOf(",")!=-1){
				String[] nmins = nmin.split(",");
				sql+=" and nmin in (";
				for(int i=0;i<nmins.length;i++){
					sql+= (i!=0) ? ","+nmins[i] : nmins[i];
				}
				sql+=")";
			}else if(nmin.length()>0)
				sql+=" and nmin = "+params.get("nmin");
		}
		if(params.containsKey("reportno")){
			String reportno = params.get("reportno");
			sql+=" and reportid in (select id from EPR_EngineerReport where reportno = '"+dbi.fixSql(reportno)+"')";
			
		}
		if(params.containsKey("engineno")){
			String enginenos = params.get("engineno");
			if(enginenos.indexOf(",")!=-1){
				String[] engineno = enginenos.split(",");
				sql+=" and engineno in (";
				for(int i=0;i<engineno.length;i++){
					sql+= (i!=0) ? ",'"+engineno[i]+"'" : "'"+engineno[i]+"'";
				}
				sql+=")";
			}else if(enginenos.length()>0)
				sql+=" and engineno = '"+dbi.fixSql(enginenos)+"'";
		}
		if(params.containsKey("spec")){
			sql+=" and reportid in (select id from EPR_EngineerReport where spec = '"+dbi.fixSql(params.get("spec"))+"')";
		}
		if(params.containsKey("code")){
			sql+=" and reportid in (select id from EPR_EngineerReport where code = '"+dbi.fixSql(params.get("code"))+"')";
		}
		
		return sql;
	}
	
	public boolean clearByRecordId(DBI dbi, int recordid) {
		boolean rs = false;
		try {
			String sql = "delete from EPR_SourceRecordDetail where recordid="+recordid;
			rs = dbi.execute(sql);
		} catch (SysException e) {
			logger.error("SourceRecordDetailDB.clearByRecordId(DBI, int) Error!", e);
		}

		return rs;
	}
	
	public boolean insert(DBI dbi, SourceRecordDetail bean) {
		try {
			return dbi
					.execute("INSERT INTO EPR_SourceRecordDetail VALUES ("+bean.getId()+", "+bean.getReportid()+", "+bean.getRecordid()+", "
							+bean.getNmin()+", "+bean.getPeo()+", "+bean.getTeo()+", "+bean.getGf()+", "
							+bean.getGe()+", "+bean.getP()+", "+bean.getPAir()+", "+bean.getPExh()+", "
							+bean.getPOil()+", "+bean.getPb()+", "+bean.getPhi()+", "+bean.getRedfac()+", "
							+bean.getTAir()+", "+bean.getTExh()+", "+bean.getTOil()+", "+bean.getTorque()+", "
							+bean.getTwi()+", "+bean.getTwo()+", "+bean.getTfuel()+", '"+bean.getEngineno()+"')");
		} catch (SysException e) {
			logger.error("SourceRecordDetailDB.insert(DBI, SourceRecordDetail) Error!", e);
			return false;
		}
	}
}
